package com.nopay.dxdwphone.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nopay.dxdwphone.bean.response.GetRichTextResponse;
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.List;
import java.util.regex.Pattern;

import com.nopay.dxdwphone.Constants;
import com.nopay.dxdwphone.R;

/**
 * author：jun
 * date：2021/7/20
 */
public class VideoCourseAdapter extends BaseQuickAdapter<GetRichTextResponse.Data, BaseViewHolder> {

    private final Context mContext;

    public VideoCourseAdapter(@Nullable Context context, @Nullable List<GetRichTextResponse.Data> data) {
        super(R.layout.adapter_video_course, data);
        mContext = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, GetRichTextResponse.Data bean) {
        holder.setText(R.id.tv_title, bean.getTitle());
        StandardGSYVideoPlayer player = holder.getView(R.id.gsy_player);
        player.setUpLazy(dealUrl(bean.getContent()), true, null, null, bean.getTitle());
        player.getFullscreenButton().setOnClickListener(v -> {
            player.startWindowFullscreen(mContext, false, true);
//            ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        });
        player.setAutoFullWithSize(true);
        player.setShowFullAnimation(true);
        player.getTitleTextView().setVisibility(View.GONE);
        player.getBackButton().setVisibility(View.GONE);

        player.setVideoAllCallBack(new VideoAllCallBack() {
            @Override
            public void onStartPrepared(String url, Object... objects) {

            }

            @Override
            public void onPrepared(String url, Object... objects) {
                new Handler().postDelayed(() -> player.startWindowFullscreen(mContext, false, true), 200);
            }

            @Override
            public void onClickStartIcon(String url, Object... objects) {

            }

            @Override
            public void onClickStartError(String url, Object... objects) {

            }

            @Override
            public void onClickStop(String url, Object... objects) {

            }

            @Override
            public void onClickStopFullscreen(String url, Object... objects) {

            }

            @Override
            public void onClickResume(String url, Object... objects) {

            }

            @Override
            public void onClickResumeFullscreen(String url, Object... objects) {

            }

            @Override
            public void onClickSeekbar(String url, Object... objects) {

            }

            @Override
            public void onClickSeekbarFullscreen(String url, Object... objects) {

            }

            @Override
            public void onAutoComplete(String url, Object... objects) {

            }

            @Override
            public void onComplete(String url, Object... objects) {

            }

            @Override
            public void onEnterFullscreen(String url, Object... objects) {

            }

            @Override
            public void onQuitFullscreen(String url, Object... objects) {

            }

            @Override
            public void onQuitSmallWidget(String url, Object... objects) {

            }

            @Override
            public void onEnterSmallWidget(String url, Object... objects) {

            }

            @Override
            public void onTouchScreenSeekVolume(String url, Object... objects) {

            }

            @Override
            public void onTouchScreenSeekPosition(String url, Object... objects) {

            }

            @Override
            public void onTouchScreenSeekLight(String url, Object... objects) {

            }

            @Override
            public void onPlayError(String url, Object... objects) {

            }

            @Override
            public void onClickStartThumb(String url, Object... objects) {

            }

            @Override
            public void onClickBlank(String url, Object... objects) {

            }

            @Override
            public void onClickBlankFullscreen(String url, Object... objects) {

            }
        });
    }

    private boolean isHttp(String url) {
        if(url != null) {
            return Pattern.matches("^((https|http|ftp|rtsp|mms)?://).*", url);
        }
        return false;
    }

    private String dealUrl(String url) {
        if(isHttp(url)) {
            return url;
        }

        if(url.matches("^/.*")) {
            url = url.replaceFirst("/", "");
        }
        return Constants.BASE_URL + url;
    }
}
